/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (c) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package res.players;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;

import plar.core.Action;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.Player;
import org.jbox2d.common.Vec2;

public class KittyCat extends Player {

    public class SpriteManager implements Action {
        int firing = 0;

        int index = 0;
        int maxFiring = 50;
        HashMap<String, ArrayList<Sprite>> spriteSet;

        public SpriteManager() {
            spriteSet = new HashMap<String, ArrayList<Sprite>>();
            spriteSet.put("move_right", new ArrayList());
            spriteSet.put("move_left", new ArrayList());
            spriteSet.put("jump_right", new ArrayList());
            spriteSet.put("jump_left", new ArrayList());
            spriteSet.put("stop_right", new ArrayList());
            spriteSet.put("stop_left", new ArrayList());
            spriteSet.put("fire_left", new ArrayList());
            spriteSet.put("fire_right", new ArrayList());
            spriteSet.put("jumpfire_left", new ArrayList());
            spriteSet.put("jumpfire_right", new ArrayList());
			spriteSet.put("down_left", new ArrayList());
            spriteSet.put("down_right", new ArrayList());
            String p = "res/players/kittycat/";
            spriteSet.get("stop_right").add(
                    new Sprite(p+"kittysr.png", 30));
            spriteSet.get("stop_left").add(
                    new Sprite(p+"kittysl.png", 30));
            spriteSet.get("move_right").add(
                    new Sprite(p+"kittyrr0.png", 30));
            spriteSet.get("move_right").add(
                    new Sprite(p+"kittyrr1.png", 30));
            spriteSet.get("move_right").add(
                    new Sprite(p+"kittyrr2.png", 30));
            spriteSet.get("move_right").add(
                    new Sprite(p+"kittyrr3.png", 30));
            spriteSet.get("move_left").add(
                    new Sprite(p+"kittyrl0.png", 30));
            spriteSet.get("move_left").add(
                    new Sprite(p+"kittyrl1.png", 30));
            spriteSet.get("move_left").add(
                    new Sprite(p+"kittyrl2.png", 30));
            spriteSet.get("move_left").add(
                    new Sprite(p+"kittyrl3.png", 30));
            spriteSet.get("jump_left").add(
                    new Sprite(p+"kittyrl1.png", 30));
            spriteSet.get("jump_right").add(
                    new Sprite(p+"kittyrr1.png", 30));
            spriteSet.get("down_left").add(
                    new Sprite(p+"kittydl.png", 30));
            spriteSet.get("down_right").add(
                    new Sprite(p+"kittydr.png", 30));
            spriteSet.get("fire_left").add(
         new Sprite(p+"kittyrlf.png", 30));
            spriteSet.get("fire_right").add(
                    
                               new Sprite(p+"kittyrrf.png", 30));
        }

        public Parameters call(Parameters parameters) {

            Integer e = (Integer) parameters.get("life");
            Player me = (Player) parameters.get("me");
            String subAction = (String) parameters.get("subAction");

            // Common.info("keen: "+subAction);
            // if(subAction == "guiRefresh")
            if (subAction == "main") {
                Double direction = (Double) me.getProperty("direction");
                boolean isJumping = (getSpeed().y != 0);
                boolean isRunning = (getSpeed().x != 0);
                Boolean isFiring = (Boolean) getProperty("isFiring");
                Boolean isDown = (Boolean) getProperty("isDown");
               if(isDown==null) isDown=false;
                // Common.info("is firing! :"+isFiring);
                if (firing > maxFiring)
                    firing = 0;
                if ((firing > 0 || isFiring) && direction <= 0.5) {
                        // Common.info("is firing! :"+isFiring);
                        ArrayList<Sprite> sprites = spriteSet.get("fire_right");
                        // setProperty("currentSprite",sprites.get( index %
                        // sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    
                    firing++;
                    return null;
                    }
                 else if ((firing > 0 || isFiring) && direction > 0.5) {
                        // Common.info("is firing! :"+isFiring);
                        ArrayList<Sprite> sprites = spriteSet.get("fire_left");
                        // setProperty("currentSprite",sprites.get( index %
                        // sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    
                    firing++;
                    return null;
                    
                } else if (isJumping && direction <= 0.5) {
                    ArrayList<Sprite> sprites = spriteSet.get("jump_right");
                    // setProperty("currentSprite",sprites.get( index %
                    // sprites.size()));
                    setProperty("currentSpriteSet", sprites);
                }
                if (isJumping && direction > 0.5) {
                    // Common.info("jump Speed: "+getSpeed());
                    ArrayList<Sprite> sprites = spriteSet.get("jump_left");
                    // setProperty("currentSprite",sprites.get(index%sprites.size()));
                    setProperty("currentSpriteSet", sprites);
                }

                if (isRunning && !isJumping) {
                    // Common.info("jump Speed: "+getSpeed());
                    if (direction > 0.5) {
                        ArrayList<Sprite> sprites = spriteSet.get("move_left");
                        // setProperty("currentSprite",sprites.get(index%sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    } else if (direction <= 0.5) {
                        ArrayList<Sprite> sprites = spriteSet.get("move_right");
                        // setProperty("currentSprite",sprites.get(index%sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    }
                } else if (!isRunning && !isJumping) {

                    if (direction > 0.5) {
                        ArrayList<Sprite> sprites = spriteSet.get("stop_left");
                        // setProperty("currentSprite",sprites.get(index%sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    } else {
                        ArrayList<Sprite> sprites = spriteSet.get("stop_right");
                        // setProperty("currentSprite",sprites.get(index%sprites.size()));
                        setProperty("currentSpriteSet", sprites);
                    }

                }
                 if(isDown && direction<=0.5)
                {
					ArrayList<Sprite> sprites = spriteSet.get("down_right");

                        setProperty("currentSpriteSet", sprites);
				}
				       if(isDown && direction>0.5)
                {
					ArrayList<Sprite> sprites = spriteSet.get("down_left");

                        setProperty("currentSpriteSet", sprites);
				}

            }
            if (Math.random() > 0.8)
                index++;
            return null;
        }
    }

    public KittyCat() {
        super();
        actions.addAction("actionManager", new SpriteManager());
        //Sprite s = new Sprite("res/keen/keenjr0.png", -1);
        //ArrayList<Sprite> ss = new ArrayList<Sprite>();
        //ss.add(s);
        //setProperty("currentSpriteSet", ss);
   //
        //setProperty("runSpeed", 7);
        setProperty("name", "Kitty Cat");
        setProperty("reducedSize", new Vec2(0.6f,0.35f));
        setProperty("standardSize", new Vec2(0.3f,0.7f));
        setProperty("presentation", "res/players/kittycat/kittysr.png");
        setProperty("special", "he have the multilocation ability (life is divided in all the copies!)");
        
    }
    
}
